package com.example.mydy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.adapter.UserRewardTaskAdapter;
import com.example.mydy.model.RewardTask;
import com.example.mydy.utils.MockDataManager;

import java.util.List;

/**
 * 悬赏任务列表 - 用户查看和接取悬赏任务的界面
 */
public class RewardTaskListActivity extends AppCompatActivity implements UserRewardTaskAdapter.OnTaskClickListener {

    private Button btnBack;
    private Button btnMyRewards;
    private RecyclerView rvRewardTasks;
    private LinearLayout emptyView;
    private UserRewardTaskAdapter adapter;
    private MockDataManager mockDataManager;
    private String currentUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reward_task_list);

        // 初始化数据管理器
        mockDataManager = MockDataManager.getInstance();
        currentUsername = mockDataManager.getCurrentUser().getUsername();

        // 初始化视图
        initViews();
        
        // 设置点击事件
        setupClickListeners();
        
        // 加载悬赏任务数据
        loadRewardTasks();
    }

    private void initViews() {
        btnBack = findViewById(R.id.btn_back);
        btnMyRewards = findViewById(R.id.btn_my_rewards);
        rvRewardTasks = findViewById(R.id.rv_reward_tasks);
        emptyView = findViewById(R.id.empty_view);
        
        // 设置RecyclerView
        rvRewardTasks.setLayoutManager(new LinearLayoutManager(this));
    }

    private void setupClickListeners() {
        btnBack.setOnClickListener(v -> finish());
        
        btnMyRewards.setOnClickListener(v -> {
            // 跳转到我的悬赏任务页面
            Intent intent = new Intent(this, MyRewardTasksActivity.class);
            startActivity(intent);
        });
        
        // 为空状态视图中的按钮添加点击事件 - 找出正确的按钮ID
        Button btnTakeRewards = emptyView.findViewById(R.id.btn_take_rewards);
        if (btnTakeRewards != null) {
            btnTakeRewards.setOnClickListener(v -> {
                // 刷新列表
                loadRewardTasks();
            });
        }
    }

    private void loadRewardTasks() {
        // 获取奖励任务列表
        List<RewardTask> rewardTasks = MockDataManager.getAllRewardTasksStatic();
        
        // 检查是否有数据
        if (rewardTasks.isEmpty()) {
            rvRewardTasks.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        } else {
            rvRewardTasks.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
            
            // 设置适配器
            adapter = new UserRewardTaskAdapter(this, rewardTasks, this);
            rvRewardTasks.setAdapter(adapter);
        }
    }

    @Override
    public void onTaskClick(RewardTask task) {
        // 打开任务详情
        // 实际应用中可以跳转到详情页面
        Toast.makeText(this, "查看任务: " + task.getTitle(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTaskAccept(RewardTask task) {
        // 用户接取任务
        boolean success = mockDataManager.takeRewardTask(currentUsername, task.getId());
        
        if (success) {
            Toast.makeText(this, "成功接取任务: " + task.getTitle(), Toast.LENGTH_SHORT).show();
            // 刷新数据
            loadRewardTasks();
        } else {
            Toast.makeText(this, "接取任务失败，请稍后重试", Toast.LENGTH_SHORT).show();
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // 每次恢复时刷新列表，以显示最新状态
        loadRewardTasks();
    }
} 